package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetMealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Order;
import com.itheima.health.pojo.OrderDto;
import com.itheima.health.pojo.OrderNewDto;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetMealService;
import javafx.scene.input.DataFormat;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

/**
 * @author zhangmeng
 * @description 套餐SEVICE实现类
 * @date 2019/9/26
 **/
@Service
@Slf4j
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetMealDao setMealDao;

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        log.info("[套餐-添加]data:{},checkgroupIds:{}", setmeal, checkgroupIds);
        // 调用DAO数据入库
        // 插入基本数据
        setMealDao.insert(setmeal);
        // 插入关联数据
        for (Integer checkgroupId : checkgroupIds) {
            setMealDao.insertSetMealAndCheckGroup(setmeal.getId(), checkgroupId);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //DAO查询数据
        Page<Setmeal> page = setMealDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Setmeal> findAll() {
        List<Setmeal> SetmealList = setMealDao.selectAll();
        return SetmealList;
    }


    @Override
    public Setmeal selectById(Integer id) {
        Setmeal setmeal = setMealDao.selectById(id);
        return setmeal;
    }


    @Override
    @Transactional
    public void addOrder(OrderDto orderDto) {
        //获取要预约的时间
        Date orderDate = orderDto.getOrderDate();
        //根据预约时间查询当天设置的最多人数
        Integer number = setMealDao.selectOrderSetting(orderDate);
        //根据预约时间查询当天已经预约的人数
        Integer reservations = setMealDao.selectReservations(orderDate);
        //根据电话查询用户id
        Integer userID = setMealDao.selectMemberByTelphone(orderDto.getTelephone());
        //查询预约表里是否有改用户id

        Order order = setMealDao.selectOrderBy(userID);
        ////查询表里是否有改时间
        //if (setMealDao.findOrderDateByDATE(orderDate)==null) {
        //    throw new BusinessRuntimeException("当天没有预约名额请更换");
        //}
        if (setMealDao.findOrderDateByDATE(orderDate) == null) {

            throw new BusinessRuntimeException("当天没有预约名额请更换");
        } else if (number - reservations > 0 && order == null) {

            Long telephone = orderDto.getTelephone();
            Integer memberID = setMealDao.selectMemberByTelphone(telephone);
            Date date = orderDto.getOrderDate();
            String orderStatus = orderDto.getOrderStatus();
            Integer setmealId = orderDto.getSetmealId();
            String name = orderDto.getName();
            //修改member表的name
            setMealDao.updateUserName(name);

            System.out.println("data:" + orderDto.getOrderDate());
            //order.setOrderDate(orderDto.getOrderDate());

            setMealDao.addOrder(orderDto, memberID, date, Order.ORDERTYPE_WEIXIN, orderStatus, setmealId);

            //修改member表的name
            setMealDao.updateUserName(name);
            //查询用户id
            Integer memberId = setMealDao.selectMemberByTelphone(orderDto.getTelephone());
            //根据用id查询预定id
            Integer orderID = setMealDao.selectOrderIdByUserId(memberId);
            String memberName = setMealDao.selectMemberNameByTelphone(orderDto.getTelephone());
            orderDto.setName(memberName);
            orderDto.setId(orderID);
        } /*else if (setMealDao.findOrderDateByDATE(orderDate)==null){
      //    throw new BusinessRuntimeException("当天没有预约名额请更换");*/
         else {

            order.setOrderDate(orderDto.getOrderDate());
            String name = orderDto.getName();
            //修改member表的name
            setMealDao.updateUserName(name);
            //查询用户id
            Integer memberId = setMealDao.selectMemberByTelphone(orderDto.getTelephone());
            //根据用id查询预定id
            Integer orderID = setMealDao.selectOrderIdByUserId(memberId);
            String memberName = setMealDao.selectMemberNameByTelphone(orderDto.getTelephone());
            orderDto.setName(memberName);
            orderDto.setId(orderID);
            //throw new BusinessRuntimeException("已经预约请勿重复预约");
        }



    }

    @Override
    public OrderNewDto findById(Integer id) {
        OrderDto orderDto = new OrderDto();
        OrderNewDto orderNewDto = new OrderNewDto();
        //根据id查询套餐id
        Integer setmealId = setMealDao.findSetmealId(id);
        //根据套餐id查询套餐名字
        String setmealName = setMealDao.selectSetmealName(setmealId);
        //orderDto.setSetMealName(setmealName);
       //通过orderid查询memberid
        Integer memberId = setMealDao.findMemberId(id);
        //通过用户id查询名字
        String memberName = setMealDao.findNameByMemberId(memberId);
        //预定订单信息没有名字体验套餐
        Order order = setMealDao.findById(id);
        orderNewDto.setOrderType(order.getOrderType());
        orderNewDto.setSetmeal(setmealName);
        orderNewDto.setMember(memberName);
        String format = new SimpleDateFormat("yyyy-MM-dd").format(order.getOrderDate());
        orderNewDto.setOrderDate(format);

        return orderNewDto;
        //return setMealDao.findById(id);
    }

    @Override
    public Long countByImg(String img) {
            return setMealDao.countByImg(img);
    }
}
